home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Objects / rangeobject.c < prev    next >
C/C++ Source or Header  |  1998-01-26  |  6KB  |  268 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Range object implementation */
  33.  
  34. #include "Python.h"
  35.  
  36. typedef struct {
  37.     PyObject_HEAD
  38.     long    start;
  39.     long    step;
  40.     long    len;
  41.     int    reps;
  42. } rangeobject;
  43.  
  44. #include "protos/rangeobject_protos.h"
  45.  
  46.  
  47. PyObject *
  48. PyRange_New(start, len, step, reps)
  49.     long start, len, step;
  50.     int reps;
  51. {
  52.     rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
  53.  
  54.     obj->start = start;
  55.     obj->len   = len;
  56.     obj->step  = step;
  57.     obj->reps  = reps;
  58.  
  59.     return (PyObject *) obj;
  60. }
  61.  
  62. static void
  63. range_dealloc(r)
  64.     rangeobject *r;
  65. {
  66.     PyMem_DEL(r);
  67. }
  68.  
  69. static PyObject *
  70. range_item(r, i)
  71.     rangeobject *r;
  72.     int i;
  73. {
  74.     if (i < 0 || i >= r->len * r->reps) {
  75.         PyErr_SetString(PyExc_IndexError,
  76.                 "range object index out of range");
  77.         return NULL;
  78.     }
  79.  
  80.     return PyInt_FromLong(r->start + (i % r->len) * r->step);
  81. }
  82.  
  83. static int
  84. range_length(r)
  85.     rangeobject *r;
  86. {
  87.     return r->len * r->reps;
  88. }
  89.  
  90. static int
  91. range_print(r, fp, flags)
  92.     rangeobject *r;
  93.     FILE *fp;
  94.     int flags;
  95. {
  96.     int i, j;
  97.  
  98.     fprintf(fp, "(");
  99.     for (i = 0; i < r->reps; ++i)
  100.         for (j = 0; j < r->len; ++j) {
  101.             if (j > 0 || i > 0)
  102.                 fprintf(fp, ", ");
  103.  
  104.             fprintf(fp, "%ld", r->start + j * r->step);
  105.         }
  106.  
  107.     if (r->len == 1 && r->reps == 1)
  108.         fprintf(fp, ",");
  109.     fprintf(fp, ")");
  110.     return 0;
  111. }
  112.  
  113. static PyObject *
  114. range_repr(r)
  115.     rangeobject *r;
  116. {
  117.     char buf[80];
  118.     sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)",
  119.             r->start,
  120.             r->start + r->len * r->step,
  121.             r->step,
  122.             r->reps);
  123.     return PyString_FromString(buf);
  124. }
  125.  
  126. static PyObject *
  127. range_concat(r, obj)
  128.     rangeobject *r;
  129.     PyObject *obj;
  130. {
  131.     PyErr_SetString(PyExc_TypeError, "cannot concatenate range objects");
  132.     return NULL;
  133. }
  134.  
  135. static PyObject *
  136. range_repeat(r, n)
  137.     rangeobject *r;
  138.     int n;
  139. {
  140.     if (n < 0)
  141.         return (PyObject *) PyRange_New(0, 0, 1, 1);
  142.  
  143.     else if (n == 1) {
  144.         Py_INCREF(r);
  145.         return (PyObject *) r;
  146.     }
  147.  
  148.     else
  149.         return (PyObject *) PyRange_New(
  150.                         r->start,
  151.                         r->len,
  152.                         r->step,
  153.                         r->reps * n);
  154. }
  155.  
  156. static int
  157. range_compare(r1, r2)
  158.     rangeobject *r1, *r2;
  159. {
  160.     if (r1->start != r2->start)
  161.         return r1->start - r2->start;
  162.  
  163.     else if (r1->step != r2->step)
  164.         return r1->step - r2->step;
  165.  
  166.     else if (r1->len != r2->len)
  167.         return r1->len - r2->len;
  168.  
  169.     else
  170.         return r1->reps - r2->reps;
  171. }
  172.  
  173. static PyObject *
  174. range_slice(r, low, high)
  175.     rangeobject *r;
  176.     int low, high;
  177. {
  178.     if (r->reps != 1) {
  179.         PyErr_SetString(PyExc_TypeError,
  180.                 "cannot slice a replicated range");
  181.         return NULL;
  182.     }
  183.     if (low < 0)
  184.         low = 0;
  185.     else if (low > r->len)
  186.         low = r->len;
  187.     if (high < 0)
  188.         high = 0;
  189.     if (high < low)
  190.         high = low;
  191.     else if (high > r->len)
  192.         high = r->len;
  193.  
  194.     if (low == 0 && high == r->len) {
  195.         Py_INCREF(r);
  196.         return (PyObject *) r;
  197.     }
  198.  
  199.     return (PyObject *) PyRange_New(
  200.                 low * r->step + r->start,
  201.                 high - low,
  202.                 r->step,
  203.                 1);
  204. }
  205.  
  206. static PyObject *
  207. range_tolist(self, args)
  208. rangeobject *self;
  209. PyObject *args;
  210. {
  211.     PyObject *thelist;
  212.     int j;
  213.     int len = self->len * self->reps;
  214.  
  215.     if (! PyArg_Parse(args, ""))
  216.         return NULL;
  217.  
  218.     if ((thelist = PyList_New(len)) == NULL)
  219.         return NULL;
  220.  
  221.     for (j = 0; j < len; ++j)
  222.         if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
  223.             self->start + (j % self->len) * self->step))) < 0)
  224.             return NULL;
  225.  
  226.     return thelist;
  227. }
  228.  
  229. static PyObject *
  230. range_getattr(r, name)
  231.     rangeobject *r;
  232.     char *name;
  233. {
  234.     static PyMethodDef range_methods[] = {
  235.         {"tolist",    (PyCFunction)range_tolist},
  236.         {NULL,        NULL}
  237.     };
  238.  
  239.     return Py_FindMethod(range_methods, (PyObject *) r, name);
  240. }
  241.  
  242. static PySequenceMethods range_as_sequence = {
  243.     (inquiry)range_length, /*sq_length*/
  244.     (binaryfunc)range_concat, /*sq_concat*/
  245.     (intargfunc)range_repeat, /*sq_repeat*/
  246.     (intargfunc)range_item, /*sq_item*/
  247.     (intintargfunc)range_slice, /*sq_slice*/
  248.     0,        /*sq_ass_item*/
  249.     0,        /*sq_ass_slice*/
  250. };
  251.  
  252. PyTypeObject PyRange_Type = {
  253.     PyObject_HEAD_INIT(&PyType_Type)
  254.     0,            /* Number of items for varobject */
  255.     "xrange",        /* Name of this type */
  256.     sizeof(rangeobject),    /* Basic object size */
  257.     0,            /* Item size for varobject */
  258.     (destructor)range_dealloc, /*tp_dealloc*/
  259.     (printfunc)range_print, /*tp_print*/
  260.     (getattrfunc)range_getattr, /*tp_getattr*/
  261.     0,            /*tp_setattr*/
  262.     (cmpfunc)range_compare, /*tp_compare*/
  263.     (reprfunc)range_repr, /*tp_repr*/
  264.     0,            /*tp_as_number*/
  265.     &range_as_sequence,    /*tp_as_sequence*/
  266.     0,            /*tp_as_mapping*/
  267. };
  268.